package Logcheck;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import serve.Constant;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class User_signup_check
{

    //检测用户名长度
    public static boolean username_len_check(String username)
    {
        if((username.length() < CheckConstant.Min_username_len) || (username.length() > CheckConstant.Max_username_len))
        {
            //System.out.println("用户名长度应为" + CheckConstant.Min_username_len + "~" + CheckConstant.Max_username_len +"字符");
            return false;
        }
        return true;
    }

    //检测用户密码长度
    public static boolean userpasswd_len_check(String userpasswd)
    {
        if((userpasswd.length() < CheckConstant.Min_userpasswd_len) || (userpasswd.length() > CheckConstant.Max_userpasswd_len))
        {
            //System.out.println("密码长度应为" + CheckConstant.Min_userpasswd_len + "~" + CheckConstant.Max_userpasswd_len +"字符");
            return false;
        }
        return true;
    }

    //检测用户名和密码字符
    public static boolean user_chat_check(String userinf)
    {
        for(int i=0;i<userinf.length();i++)
        {
            if(!(userinf.charAt(i)>='a'&&userinf.charAt(i)<='z') && !(userinf.charAt(i)>='A'&&userinf.charAt(i)<='Z') && !(userinf.charAt(i)>='0'&&userinf.charAt(i)<='9') && userinf.charAt(i)!='.' && userinf.charAt(i)!='_')
            {
                //System.out.println("用户名和密码只能是字母、数字、点(.)和下划线(_)中的一种或多种");
                return false;
            }
        }
        return true;
    }

    public static JSONObject checkUsername(DatagramPacket datagramPacket)
    {
        JSONObject jsonObject = JSONObject.parseObject(new String(datagramPacket.getData()));

        JSONObject send_json = new JSONObject();
        int code = 0;
        String title = "";
        String message = "";

        String username = jsonObject.getString("username");

        if(username_len_check(username))
        {
            if((username.charAt(0)>='a'&&username.charAt(0)<='z')||(username.charAt(0)>='A'&&username.charAt(0)<='Z'))
            {
                if(user_chat_check(username))
                {
                    boolean is_user_exist;
                    try
                    {
                        String sql = "select * from %s where user_name=\"%s\"";
                        sql = String.format(sql,CheckConstant.usertable,username);
                        Statement stmt = Constant.chatdbConnector.getChatdb_connection().createStatement();
                        ResultSet res = stmt.executeQuery(sql);
                        is_user_exist = res.next();
                        if(is_user_exist)
                        {
                            title = CheckConstant.username_exist_error;
                            code = CheckConstant.False;
                            message = CheckConstant.username_exist_message;
                        }
                        else
                        {
                            title = CheckConstant.username_legal;
                            code = CheckConstant.True;
                            message = CheckConstant.username_legal;
                        }
                    }catch (SQLException e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    title = CheckConstant.username_chat_error;
                    code = CheckConstant.False;
                    message = CheckConstant.username_chat_message;
                }
            }
            else
            {
                title = CheckConstant.username_first_error;
                code = CheckConstant.False;
                message = CheckConstant.username_first_message;
            }
        }
        else
        {
            title = CheckConstant.userpasswd_len_error;
            code = CheckConstant.False;
            message = CheckConstant.username_len_message;
        }

        send_json.put("title",title);
        send_json.put("code",code);
        send_json.put("message",message);
        return send_json;
    }

    public static JSONObject CheckUsername(String username)
    {
        JSONObject jsonObject = new JSONObject();
        int code = 0;
        String message = "";

        if(username_len_check(username))
        {
            if((username.charAt(0)>='a'&&username.charAt(0)<='z')||(username.charAt(0)>='A'&&username.charAt(0)<='Z'))
            {
                if(user_chat_check(username))
                {
                    boolean is_user_exist;
                    try
                    {
                        String sql = "select * from %s where user_name=\"%s\"";
                        sql = String.format(sql,CheckConstant.usertable,username);
                        Statement stmt = Constant.chatdbConnector.getChatdb_connection().createStatement();
                        ResultSet res = stmt.executeQuery(sql);
                        is_user_exist = res.next();
                        if(is_user_exist)
                        {
                            code = CheckConstant.False;
                            message = CheckConstant.username_exist_error;
                        }
                        else
                        {
                            code = CheckConstant.True;
                            message = CheckConstant.username_legal;
                        }
                    }catch (SQLException e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    code = CheckConstant.False;
                    message = CheckConstant.username_chat_error;
                }
            }
            else
            {
                code = CheckConstant.False;
                message = CheckConstant.username_first_error;
            }
        }
        else
        {
            code = CheckConstant.False;
            message = CheckConstant.username_len_error;
        }

        jsonObject.put("code",code);
        jsonObject.put("message",message);

        return jsonObject;
    }

    public static JSONObject CheckUserpasswd(String userpasswd)
    {
        JSONObject jsonObject = new JSONObject();
        int code = 0;
        String message = "";

        if(userpasswd_len_check(userpasswd))
        {

            if(user_chat_check(userpasswd))
            {
                code = CheckConstant.True;
                message = CheckConstant.userpasswd_legal;
            }
            else
            {
                code = CheckConstant.False;
                message = CheckConstant.userpasswd_chat_error;
            }
        }
        else
        {
            code = CheckConstant.False;
            message = CheckConstant.userpasswd_len_error;
        }

        jsonObject.put("code",code);
        jsonObject.put("message",message);

        return jsonObject;
    }
}
